package giit.computerbase.server;

import giit.computerbase.dao.MySQL;
import giit.computerbase.dao.SQL;
import giit.computerbase.util.Application;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * User: tum0r
 * Package Name: giit.computerbase.server
 * Time: 2018-11-28 Wednesday 15:03:01
 */
public class ActivityTypeAnalyzation {
    private SQL sql;

    public ActivityTypeAnalyzation() {
        try {
            sql = new MySQL();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public HashMap<String, Integer> getAnalyzation() {
        sql.open((String) Application.getConfig().databaseConfig.get("address"),
                (int) Application.getConfig().databaseConfig.get("port"),
                Application.DATABASE_NAME,
                (String) Application.getConfig().databaseConfig.get("encode"),
                (String) Application.getConfig().databaseConfig.get("user"),
                (String) Application.getConfig().databaseConfig.get("password"));

        HashMap<String, Integer> result = new HashMap<>();

        ArrayList<HashMap<String, Object>> temp = sql.query("select * from activity_type", null);
        Iterator<HashMap<String, Object>> iterator = temp.iterator();
        while (iterator.hasNext()) {
            HashMap<String, Object> type = iterator.next();
            String typeID = (String) type.get("id");
            String typeName = (String) type.get("name");
            HashMap<String, Object> number = sql.query("select count(*) from activity_information where activity_type_id=?", new Object[]{typeID}).get(0);
            Iterator<String> num = number.keySet().iterator();
            int count = Integer.parseInt(String.valueOf(number.get(num.next())));
            result.put(typeName, count);
        }

        try {
            sql.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}
